<template>
  <el-dialog
    :title="type == 'add' ? '新建自提点' : '修改自提点'"
    v-model="visible"
    width="450px"
    center
  >
    <el-form :model="ruleForm" :rules="rules" ref="formRef" label-width="100px" class="good-form">
      <el-form-item label="自提点名称" prop="zitiName">
        <el-input type="text" v-model="ruleForm.zitiName" placeholder="请输入自提点名称"></el-input>
      </el-form-item>
      <el-form-item label="联系电话" prop="mobile">
        <el-input type="text" v-model="ruleForm.mobile" placeholder="请输入联系电话"></el-input>
      </el-form-item>
       <el-form-item label="选择地址">
          <el-cascader ref='addressRef' :props="adprops" v-model="ruleForm.area" @change="handleChangeArea" placeholder="请选择地区"></el-cascader>
         <!-- <Address ref='area' :areaStore="areaStore" /> -->
      </el-form-item>
      <el-form-item label="具体地址" prop="address">
        <el-input type="text" v-model="ruleForm.address" placeholder="请输入详细地址"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { reactive, ref, toRefs } from 'vue'
import {getData} from '@/utils/axios'
// import Address from '@/components/Address.vue'
import { ElMessage } from 'element-plus'
import { store } from '@/store'
export default {
  name: 'DialogAddCategory',//新建自提点
   components: {
    //Address
  },
  props: {
    type: String,
    reload: Function
  },
  setup(props) {
    const addressRef = ref(null)
    const formRef = ref(null)
    const state = reactive({
      visible: false,
      busbaseId:store.getters.busId,
      loginName:store.getters.userName,
      pId: '',
      ruleForm: {
        zitiName: '',
        mobile: '',
        area: '',
        address: '',
      },
      areaName:'',
      rules: {
        zitiName: [
          { required: 'true', message: '自提点名称不能为空', trigger: ['change'] }
        ],
         mobile: [
          { required: 'true', message: '联系方式不能为空', trigger: ['change'] },
          { pattern: /^1[3|4|5|6|7|8][0-9]\d{8}$/, message: '手机号格式不对', trigger: ['blur'] }
        ],
         address: [
          { required: 'true', message: '详细地址不能为空', trigger: ['change'] }
        ],
       
      },
      adprops:{
         lazy: true,
         lazyLoad(node, resolve) {
           console.log(node)
            let parentId='';
            if(node.level!==0&&node.data){
                parentId=node.data.value;
            }
            getData('common/common/getAreaHierarchyList',{parentId}).then(res=>{
              console.log(res.data)
                if(res.data.flag){
                const nodes = res.data.data.map(item=>{
                let leaf=item.areaCode%100==0?false:true // false是有子节点
                return  {
                    value: item.id,
                    label: item.name,
                    leaf: leaf
                    }
                })
                  return resolve(nodes)
                }
            }) 
        }
      },
    })
     const handleChangeArea = ( node ) => {//返回最后的id值
      console.log(node,'node')
      state.areaName='';
      if(node.length>0)
      {
        console.log(addressRef.value.getCheckedNodes()[0].pathLabels,'地名')
        addressRef.value.getCheckedNodes()[0].pathLabels.forEach(element => {//获取地址名称
              state.areaName+=element
         });
      }
    }
    // 开启弹窗
    const open = (data) => {
      state.visible = true
      if (data) {//修改自提点
       let areaA=data.areaStore.path.split(',')
        state.ruleForm = {
            zitiName: data.zitiName,
            mobile: data.mobile,
            area: areaA,
            address: data.address,
        }
        state.pId=data.id;
        state.areaName=data.areaStore.displayName
      } 
    }
    // 关闭弹窗
    const close = () => {
      state.visible = false
    }
    const submitForm = () => {
      formRef.value.validate((valid) => {
        if (valid) {
           console.log(state.ruleForm)
            let {busbaseId,loginName}=state;
            let {zitiName,mobile,address}=state.ruleForm;
            let areaId=state.ruleForm.area.join(',');
            let areaStore=JSON.stringify({displayName:state.areaName,path:areaId});
            let isUse=1;
           if (props.type == 'add'){//新增
            getData('/goods/attr/addGdsZitiAddress',{loginName,busbaseId,zitiName,mobile,areaStore,address,isUse}).then(res=>{
                  console.log(res.data)
                if(res.data.flag){
                  ElMessage.success({message:'添加成功',duration:1000})
                  state.visible = false
                  if (props.reload) props.reload() // 添加成功后，更新自提点列表
                }
            }) 
            }else{//修改自提点
               let id=state.pId;
                getData('/goods/attr/updateGdsZitiAddress',{id,busbaseId,zitiName,mobile,areaStore,address,isUse}).then(res=>{
                  console.log(res.data)
                if(res.data.flag){
                  ElMessage.success({message:'修改成功',duration:1000})
                  state.visible = false
                  if (props.reload) props.reload() // 添加成功后，更新自提点列表
                }
            })
            }

          // if (props.type == 'add') {
        
          //   })
          // } else {
          //   axios.put('/categories', {
          //     categoryId: state.id,
          //     categoryLevel: state.categoryLevel,
          //     parentId: state.parentId,
          //     categoryName: state.ruleForm.name,
          //     categoryRank: state.ruleForm.rank
          //   }).then(() => {
          //     ElMessage.success('修改成功')
          //     state.visible = false
          //     if (props.reload) props.reload()
          //   })
          // }
        }
      })
    }
    return {
      ...toRefs(state),
      addressRef,
      open,
      close,
      formRef,
      submitForm,
      handleChangeArea
    }
  }
}
</script>